package com.etc.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.etc.dao.impl.SiteDaoImpl;
import com.etc.entity.Site;
import com.etc.service.SiteService;
import com.etc.util.DBUtil;

public class SiteServiceImpl implements SiteService{
	SiteDaoImpl dao = new SiteDaoImpl();
	@Override
	public List<Site> queryAllSite() {
		String sql="SELECT * FROM site";
		CachedRowSet set = DBUtil.execQuery(sql);
		List<Site> list = new ArrayList<Site>();
		Site site=null;
		try {
			while (set.next()) {
				int s_id = set.getInt(1);
				String s_name = set.getString(2);
				 site = new Site(s_id, s_name);
				 list.add(site);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public boolean addSite(Site site) {
		
		if (site!=null) {
			//不为空
		
			Site site1 = dao.querySite(site.getS_name());
			if (site1==null) {//名字不能重复
				String sql="INSERT INTO site VALUES(null,?)";
				boolean b = DBUtil.executeUpdate(sql, site.getS_name());
				return b;
			}
		}
		return false;
	}

	@Override
	public boolean rmvSite(int id) {
		 String sql="DELETE FROM site WHERE s_id=?";
	     boolean b = DBUtil.executeUpdate(sql, id);
		return b;
	}

	@Override
	public boolean upSite(Site site) {
		if (site!=null) {
			boolean b = dao.upSite(site);
			return b;
		}
		return false;
	}

}
